package cn.bjc.product.controller;

import cn.bjc.basic.exception.BusinessException;
import cn.bjc.basic.util.JsonResult;
import cn.bjc.basic.util.LoginContext;
import cn.bjc.basic.util.PageList;
import cn.bjc.product.domain.Product;
import cn.bjc.product.query.ProductQuery;
import cn.bjc.product.service.IProductService;
import cn.bjc.user.domain.Logininfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

//@Controller
//@ResponseBody
@RestController //controller层,返回json数据
@RequestMapping("/product")
//接口文档的注解
@Api(value = "产品的API",description="产品相关的CRUD功能")
public class ProductController {

    @Autowired
    private IProductService productService;

    /**
     *  查询所有:
     *  动词:get
     * @return
     */
    //@RequestMapping(value = "",method = RequestMethod.GET)
    @GetMapping()//上面的结合
    @ApiOperation(value = "查询所有")
    public List<Product> findAll(HttpServletRequest request) {
        //获取登录人信息
        Logininfo logininfo = LoginContext.getLogininfo(request);

        return productService.loadAll();
    }


    /**
     * 查询一个:接收参数id
     * url:http://localhost/Product/id/
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "通过id查询")
    public Product findOne(@PathVariable("id") long id) {
        return productService.loadById(id);
    }

    /**
     * 删除操作
     *  url:http://localhost/product/id/
     * @param id
     * @return
     * JsonResult:链式编程
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "通过id删除")
    public JsonResult deleteById(@PathVariable("id") Long id) {
        try {
            productService.remove(id);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("网络延迟,稍后重试");
        }
    }

    /**
     * 添加或修改操作
     * 
     * @param Product
     * @return
     */
    @PutMapping
    @ApiOperation(value = "添加操作接口")
    public JsonResult updateOrAdd(@RequestBody Product Product){
        try {
            if(Product.getId()==null){
                //添加操作
                productService.save(Product);
            } else {
                //修改
                productService.update(Product);
            }
            return JsonResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return JsonResult.me().setMsg(e.getMessage());
        } catch (Exception e){
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }
    }

    /**
     * 分页查询
     * 
     */
    @PostMapping
    @ApiOperation(value = "分页查询接口")
    public PageList<Product> queryPage(@RequestBody ProductQuery query){
        try {
            return productService.pageQuery(query);
        } catch (Exception e){
            e.printStackTrace();
            return new PageList<Product>();
        }
    }


    /**
     * 批量删除操作
     * @param
     * @return
     * JsonResult:链式编程
     */
    @PatchMapping()
    @ApiOperation(value = "批量删除")
    public JsonResult patchDel(@RequestBody List<Long> ids) {
        try {
            productService.patchDel(ids);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("网络延迟,稍后重试");
        }
    }

    /**
     * 上架操作
     * @param
     * @return
     * JsonResult:链式编程
     */
    @PostMapping("/onsale")
    @ApiOperation(value = "上架")
    public JsonResult onSale(@RequestBody List<Long> ids) {
        try {
            productService.onSale(ids);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("网络延迟,稍后重试");
        }
    }

    /**
     * 下架操作
     * @param
     * @return
     * JsonResult:链式编程
     */
    @PostMapping("/offsale")
    @ApiOperation(value = "上架")
    public JsonResult offSale(@RequestBody List<Long> ids) {
        try {
            productService.offSale(ids);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("网络延迟,稍后重试");
        }
    }
}
